package dubbo.server4.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import dubbo.server4.api.TestService4;
import dubbo.server4.api.domain.dto.UserInfoDto;
import dubbo.server4.api.domain.dto.WebPager;
import dubbo.server4.api.domain.entity.SysUser;
import dubbo.server4.api.domain.entity.User;
import dubbo.server4.impl.common.JdbcUtil;
import dubbo.server4.impl.dao.ISysUserDao;
import dubbo.server4.impl.dao.UserMapper;

@Service
public class TestService4Impl implements TestService4 {
	@Autowired
	ISysUserDao iSysUserDao;
	@Autowired
	UserMapper userMapper;
	@Autowired
	RedisServiceImpl redisServiceImpl;
	public String getServerName() {
		return "dubbo.server4";
	}

	public WebPager<UserInfoDto> getUserDepartmentList(String key, String departname, int pageIndex, int pageSize) {
		PageHelper.startPage(pageIndex, pageSize);
		List<UserInfoDto> list = iSysUserDao.getUserDepartmentList(key, departname);
		WebPager<UserInfoDto> webpager = new WebPager<UserInfoDto>(list, ((Page) list).getTotal());
		return webpager;
	}

	public String getValueFromRedis() {
		redisServiceImpl.set("key1", "redis test:Hello World!");
		return redisServiceImpl.get("key1");
	}

	public void DealMQ(String mq) {
		redisServiceImpl.set("mq", mq);
	}
    public String VisiteMasterAndSlave() throws Exception {
    	String ret="";
    	User user=userMapper.selectByPrimaryKey(8);
    	user.setEnable(!user.isEnable());
    	userMapper.updateByPrimaryKey(user);
    	ret+="Master:enable="+(user.isEnable()?"true":"false");
    	Thread.sleep(3000);
    	JdbcUtil jdbcUtil = new JdbcUtil("spring.datasource.db2");
    	List<Object> params=new ArrayList<Object>();
    	params.add(8);
    	List<Map<String, Object>> list=jdbcUtil.findResult("select * from user where userid=?", params);
    	boolean ret1=(boolean)list.get(0).get("enable");
    	ret+="   Slave:enable="+(ret1?"true":"false");
    	return ret;
    }
	public List<Map<String, Object>> VisitDBReadAndWriteSplite() throws Exception {
		JdbcUtil jdbcUtil = new JdbcUtil("spring.datasource.db3");
		List<Map<String, Object>> list=jdbcUtil.findResult("select * from user", null);
       return list;
	}

}
